<template>
  <!-- 对账管理 -->
  <div class="app-container">
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="100px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="订单编号">
              <el-input v-model="form.order_id" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="机构名称">
              <!-- @blur="inputBlur" -->
              <el-select
                v-model="form.hospital_id"
                filterable
                remote
                :remote-method="remoteMethod"
                clearable
                @change="handleChange"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in hospitalNameList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="客户姓名">
              <el-input v-model="form.user_name" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="客户手机号">
              <el-input v-model="form.phone" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="板块">
              <el-select v-model="form.module_id" placeholder="请选择" clearable>
                <el-option
                  v-for="item in bankuaiList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="对账类型">
              <el-select v-model="form.check_account_type" placeholder="请选择" clearable>
                <el-option
                  v-for="item in accountList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="核销时间">
              <el-date-picker
                v-model="dates"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-button style="margin-left:60px" type="primary" @click="handleSearch">查询</el-button>
              <el-button type="primary" @click="handleExport">导出</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <el-table :data="tableData" class="tableData" style="width: 100%">
        <el-table-column
          fixed
          type="index"
          label="序号"
          width="50"
          align="center"
          :index="table_index"
        />
        <el-table-column prop="order_id" label="订单编号" align="center" />
        <el-table-column prop="deal_type_name" label="交易类型" align="center" />
        <el-table-column prop="hospital_name" label="机构名称" align="center" />
        <el-table-column prop="module_name" label="板块" align="center" />
        <el-table-column prop="user_name" label="客户姓名" align="center" />
        <el-table-column prop="phone" label="客户电话号" width="120px" align="center" />
        <el-table-column prop="verification_date" label="核销时间" align="center" />
        <el-table-column prop="total_amount" label="订单金额" align="center" />
        <el-table-column prop="check_account_type_name" label="对账类型" align="center" />
        <el-table-column prop="receive_amount_detail" label="收款明细" align="center" />
        <el-table-column prop="base_type_name" label="标准结算类型" width="110px" align="center" />
        <el-table-column prop="base_standard_amount" label="标准结算金额" width="110px" align="center" />
        <el-table-column prop="change_amount" label="机构最终结算金额" width="135px" align="center" />
        <el-table-column prop="income_name" label="账户收入" align="center" />
        <el-table-column prop="outcome_name" label="账户支出" align="center" />
        <el-table-column prop="hospital_name" label="账户余额" width="100px" align="center">
          <template slot-scope="scope">
            <span>交易前:{{scope.row.before_change}}</span>
            <br />
            <span>交易后:{{scope.row.after_change}}</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.page"
            :page-sizes="page_sizes"
            :page-size="form.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  accountData,
  getOrgList,
  getModuleList,
  accountExport,
} from "@/api/accountMange";
import commonFn from "@/utils/common";
export default {
  name: "reconciliation",
  data() {
    return {
      form: {
        order_id: "",
        hospital_id: "",
        user_name: "",
        phone: "",
        module_id: "",
        check_account_type: "",
        verification_date_start: "",
        verification_date_end: "",
        page: 1,
        size: 10,
      },
      dates: "",
      hospitalNameList: [],
      hospitalArr: [],
      bankuaiList: [],
      accountList: [
        // { name: "平台全款", id: 1 },
        // { name: "平台定金机构尾款", id: 2 },
        // { name: "机构全款", id: 3 },
      ], //对账类型
      tableData: [],
      page_totals: 0,
      page_sizes: [10, 30, 50, 100],
      tableLoading: false,
    };
  },
  watch: {
    // 核销时间
    dates(val) {
      this.form.verification_date_start = val ? val[0] : "";
      this.form.verification_date_end = val ? val[1] : "";
    },
  },
  mounted() {
    this.getList(this.form);
    // 获取机构名称
    this.getHosName();
    // 获取板块
    this.getBankuai();
  },

  methods: {
    // 序号
    table_index(index) {
      return (this.form.page - 1) * this.form.size + index + 1;
    },
    // 获取列表
    async getList(opt) {
      this.tableLoading = true;
      let res = await accountData(opt);
      console.log("res", res.data);
      if (res.data.code == 200) {
        this.tableLoading = false;
        let resData = res.data.data;
        this.tableData = resData;
        this.page_totals = res.data.meta.total;
      } else {
        this.tableLoading = false;
        this.errorStatus(res.data.msg);
      }
    },
    // 获取机构名称
    async getHosName() {
      let res = await getOrgList();
      if (res.data.code == 200) {
        let resData = res.data.data;
        this.hospitalNameList = resData;
        this.hospitalArr = resData;
      } else {
        this.errorStatus(res.data.msg);
      }
    },
    async getBankuai() {
      let res = await getModuleList();
      console.log("rs", res.data);
      if (res.data.code == 200) {
        this.bankuaiList = res.data.data.module_arr;
        this.accountList = res.data.data.check_account_type_arr;
      } else {
        this.errorStatus(res.data.msg);
      }
    },
    // 远程搜索获取机构名称
    remoteMethod(query) {
      const data = { keyword: query };
      if (query != "") {
        getOrgList(data).then((res) => {
          if (res.data.code == 200) {
            let resData = res.data.data;
            if (resData.length > 0) {
              // console.log(8888);
              this.hospitalNameList = resData;
            }
          } else {
            this.errorStatus(res.data.msg);
          }
        });
      } else {
        this.hospitalNameList = this.hospitalArr;
      }
    },
    // 机构名称失焦
    inputBlur() {
      this.hospitalNameList = this.hospitalArr;
    },
    // 机构名称下拉切换
    handleChange(e) {
      // console.log("e", e);
      this.form.hospital_id = e;
      this.getHosName();
    },
    // 查询
    handleSearch() {
      this.form.page = 1;
      this.getList(this.form);
    },
    // 导出

    handleExport() {
      let datas = { ...this.form };
      delete datas.page;
      delete datas.size;
      accountExport(datas)
        .then((res) => {
          console.log("ress", res);
          commonFn.downFile("机构对账管理导出", res);
        })
        .catch((err) => {
          // this.errorStatus(res.data.msg);
        });
    },

    // 改变页面条数
    handleSizeChange(val) {
      this.form.size = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form);
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.eleMsg(txt, "error");
    },
    // 成功状态弹窗
    warnStatus(txt, status) {
      this.eleMsg(txt, status);
      // this.eleMsg(txt, "warning");
    },
    // 弹窗组件
    eleMsg(txt, status) {
      this.$message({
        showClose: true,
        message: txt,
        type: status,
      });
    },
  },
};
</script>
<style  scoped>
.pagesCon {
  height: 40px;
  margin-top: 20px;
}
.pagesCon > div {
  float: right;
}
.inp_wid {
  display: inline-block;
  width: 236px !important;
}
</style>